/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseHibernateDao;
import javacommon.util.StringUtils;

import org.springframework.stereotype.Repository;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import cn.org.rapid_framework.util.ObjectUtils;

import com.tech15.task.model.Task;
import com.tech15.task.vo.query.TaskQuery;

@Repository
public class TaskDao extends BaseHibernateDao<Task,Long>{

	public Class getEntityClass() {
		return Task.class;
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		String sql = "select t from Task t where 1=1 "
				+ "/~ and t.wfid = '[wfid]' ~/"
				+ "/~ and t.serial = '[serial]' ~/"
				+ "/~ and t.title = '[title]' ~/"
				+ "/~ and t.timelimit = '[timelimit]' ~/"
				+ "/~ and t.creater = '[creater]' ~/"
				+ "/~ and t.tel = '[tel]' ~/"
				+ "/~ and t.creattime = '[creattime]' ~/"
				+ "/~ and t.complettime = '[complettime]' ~/"
				+ "/~ and t.content = '[content]' ~/"
				+ "/~ and t.taskstate = '[taskstate]' ~/"
				+ "/~ and t.curnode = '[curnode]' ~/"
				+ "/~ and t.exectype = '[exectype]' ~/"
				+ "/~ order by [sortColumns] ~/";
		return pageQuery(sql,pageRequest);
	}
	
	public Page findPage(TaskQuery query) {
		String sql = "select t.*,t1.name as dataname,t2.realname as username from Task t " +
				" left join pub_data t1 on t.lb=t1.id " +
				" left join sys_user t2 on t.creater=t2.userid " +
				" left join sys_dept t3 on t2.deptid=t3.deptid where 1=1 ";
		
				if(ObjectUtils.isNotEmpty(query.getDeptid())){
					sql += " and t3.deptid="+query.getDeptid();
				}
				sql += "/~ and t2.deptname like '%[deptname]%' ~/"
				+ "/~ and t.serial like '%[serial]%' ~/"
				+ "/~ and t.title like '%[title]%' ~/"
				+ "/~ and t.zsbmlxr like '%[zsbmlxr]%' ~/"
				+ "/~ and t.creater = [creater] ~/"
				+ "/~ and t.creattime = '[creattime]' ~/"
				+ "/~ and t.complettime = '[complettime]' ~/"
				+ "/~ and t.content = '[content]' ~/"
				+ "/~ and t.taskstate = [taskstate] ~/"
				+ "/~ and t.execstate = [execstate] ~/"
				+ "/~ and t.lb = [lb] ~/"
				+ "/~ and t.taskstate = [taskwczt] ~/"
				+ "/~ and t.execstate = [taskwczt] ~/"
				//+ "/~ and t.taskstate <> [taskzt] ~/"
				+ "/~ and t.execstate <> [taskzt] ~/";
				//+ "/~ order by [sortColumns] ~/";
				sql += " order by execstate asc, creattime desc";
				
		Map map = new HashMap();
		map.put("taskid", "Long");
		map.put("serial", "String");
		map.put("title", "String");
		map.put("creater", "Long");
		map.put("creattime", "Timestamp");
		map.put("authtime", "Timestamp");
		map.put("complettime", "Timestamp");
		map.put("jhwcsj", "Timestamp");
		map.put("zsbmlxr", "String");
		map.put("taskstate", "Integer");
		map.put("execstate", "Integer");
		map.put("lb", "Long");
		map.put("username", "String");
		map.put("dataname", "String");
		return pageSqlQuery(sql, query, map, Task.class);
	}

	public Page myTask(TaskQuery query) {
		String sql = "select t1.tpid,t1.hanlder,t.*,t2.realname as username,t3.name as dataname from Task_process t1 " +
				" left join task t on t1.taskid=t.taskid " +
				" left join sys_user t2 on t.creater=t2.userid " +
				" left join pub_data t3 on t.lb=t3.id where 1=1 "
			+ "/~ and t1.hanlder = [hanlder] ~/"
			+ "/~ and t1.hanldestate = [hanldestate] ~/"
			+ "/~ and t.serial like '%[serial]%' ~/"
			+ "/~ and t.title = '[title]' ~/"
			+ "/~ and t.zsbmlxr like '%[zsbmlxr]%' ~/"
			+ "/~ and t.timelimit = '[timelimit]' ~/"
			+ "/~ and t.creater = [creater] ~/"
			+ "/~ and t.creattime = '[creattime]' ~/"
			+ "/~ and t.complettime = '[complettime]' ~/"
			+ "/~ and t.content = '[content] ~/"
			+ "/~ and t.taskstate = [taskstate] ~/"
			+ "/~ and t.execstate = [execstate] ~/"
			+ "/~ order by [sortColumns] ~/";
			
		Map map = new HashMap();
		map.put("tpid", "Long");
		map.put("taskid", "Long");
		map.put("serial", "String");
		map.put("title", "String");
		map.put("creater", "Long");
		map.put("jhwcsj", "Timestamp");
		map.put("zsbmlxr", "String");
		map.put("hanlder", "Long");
		map.put("creattime", "Timestamp");
		map.put("taskstate", "Integer");
		map.put("execstate", "Integer");
		map.put("lb", "Long");
		map.put("username", "String");
		map.put("dataname", "String");
		return pageSqlQuery(sql, query, map, Task.class);
	}
	
	
	public Page mySearch(TaskQuery query) {
		String sql = "select t.*,t1.realname as username,t2.name as dataname from (" +
				"select t.*,(select count(*) from task_process a where a.taskid=t.taskid and a.hanldestate=1 " +
				" and a.hanlder="+query.getHanlder()+") as mycls from task t " +
				") t " +
				" left join sys_user t1 on t.creater=t1.userid " +
				" left join pub_data t2 on t.lb=t2.id where (t.mycls>0 or creater="+query.getHanlder()+") "
				+ "/~ and t.serial like '%[serial]%' ~/"
			+ "/~ and t.title = '[title]' ~/"
			+ "/~ and t.zsbmlxr like '%[zsbmlxr]%' ~/"
			+ "/~ and t.timelimit = '[timelimit]' ~/"
			+ "/~ and t.creater = [creater] ~/"
			+ "/~ and t.creattime = '[creattime]' ~/"
			+ "/~ and t.complettime = '[complettime]' ~/"
			+ "/~ and t.content = '[content] ~/"
			+ "/~ and t.taskstate = [taskstate] ~/"
			+ "/~ and t.execstate = [execstate] ~/"
			+ "/~ order by [sortColumns] ~/";
			
		Map map = new HashMap();
		map.put("taskid", "Long");
		map.put("serial", "String");
		map.put("title", "String");
		map.put("creater", "Long");
		map.put("jhwcsj", "Timestamp");
		map.put("zsbmlxr", "String");
		map.put("creattime", "Timestamp");
		map.put("taskstate", "Integer");
		map.put("execstate", "Integer");
		map.put("lb", "Long");
		map.put("username", "String");
		map.put("dataname", "String");
		return pageSqlQuery(sql, query, map, Task.class);
	}
	
	
	public String searchMaxRwbh() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String head = "FD";
		String st = sdf.format(new Date()).toString();
		String sql = "select max(t.serial) from Task t where to_char(t.creattime,'yyyy') = '"+st+"' ";
		List list = getHibernateTemplate().find(sql);
		if(list != null && list.size()>0){
			String maxnum = (String)list.get(0);
			if(StringUtils.notNull(maxnum)){
				int size = Integer.parseInt(maxnum.split("-")[1]);
				if(size < 9){
					return head+st + "-" + "000"+(size+1);
				}else if(size>=9 && size<99){
					return head+st + "-" + "00"+(size+1);
				}else if(size>=99 && size<999){
					return head+st + "-" + "0"+(size+1);
				}else{
					return head+st + "-" + (size+1);
				}
			}else{
				return head+st + "-" + "0001";
			}
		}else{
			return head+st + "-" + "0001";
		}
	}
	

}
